package com.example.administrator.meishi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.administrator.meishi.bean.Video;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;


public class Movie extends AppCompatActivity {

    private VideoView vv;
    private TextView tv;
    private Video video;
    private String name;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String video1;

    //用于进行电影播放和收藏
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        tv = (TextView) findViewById(R.id.text_zi);
        vv = (VideoView) findViewById(R.id.video_view);
        Intent intent=getIntent();
        sp = getSharedPreferences("meishi",MODE_PRIVATE);
        editor = sp.edit();
        video = (Video) intent.getSerializableExtra("video");
        name = video.getRecommend_caption();
        video1 = this.video.getMedia().getVideo();
            vv.setVideoURI(Uri.parse(video1));
            vv.start();
        tv.setText(name);
        }
    //存储数据
    public void sc(View view) {
        if (ishava()){
           Toast.makeText(Movie.this,"亲，您之前收藏过了",Toast.LENGTH_SHORT).show();
        }else {
            Set<String> stringset = sp.getStringSet(name, null);
            Set<String>set=new HashSet<>();
            if (stringset!=null){
                Toast.makeText(Movie.this,"收藏成功",Toast.LENGTH_SHORT).show();
            }else {
                set.add(name);
                set.add(video1);
                editor.putStringSet(name,set);
                editor.commit();
                if (editor.commit()){
                    Toast.makeText(Movie.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(Movie.this, "收藏失败", Toast.LENGTH_SHORT).show();
                }
        }

        }
    }
    private boolean ishava(){
        Map<String, ?> msp = sp.getAll();
        Set<String> keySet = msp.keySet();
        boolean flog=false;
        if (keySet.contains(name)){
            flog=true;
        }
        return flog;
    }
    }

